<template>
  <div>
    <!-- 搜索框 -->
    <TopFilter ref="topFilter" :list="topFilterList" :cols="3" labelWidth="90" :collapse="false" @filterChange="changeHandle" @onCollapse="collapseHandle"></TopFilter>

    <!-- 问卷信息 -->
    <div class="box">
      问卷信息
      <FilterTable
        ref="table"
        :columns="columns"
        :fetchapi="fetchApi"
        :params="fetchParams"
        :datakey="'rows'"
        :height="'500px'"
        :isSelectColumn="false"
        :isToperInfo="false"
        columnsRef="wjdyWjWjxxTable"
        :onPageChange="clearQuestionTable"
        :onColumnsChange="columns => (this.columns = columns)"
      >
        <template slot="controls">
          <el-button size="small" type="primary" icon="el-icon-plus" @click="add()">新增</el-button>
        </template>
      </FilterTable>
    </div>

    <!-- 问题信息 -->
    <div class="box">
      问题信息
      <FilterTable
        ref="questionTable"
        :columns="questionTableColums"
        :fetchapi="getQuestionApi"
        :params="fetchQuestionParams"
        :datakey="'rows'"
        :isSelectColumn="false"
        :isToperInfo="false"
        :isColumnFilter="false"
        :height="'500px'"
        columnsRef="wjdyWjWtxxTable"
        :onSyncTableData="tableChangeFun"
        :onColumnsChange="columns => (this.questionTableColums = columns)"
      >
      </FilterTable>
    </div>

    <!-- 新增/编辑问卷 -->
    <Drawer
      :visible.sync="visible"
      :title="(type == 'add' ? '新增' : '编辑') + '问卷'"
      destroyOnClose
      :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px', width: '1200px' }"
    >
      <QuestionnairePanel :type="type" :questionnaireId="editQuestionnaireId" @changeHandle="changeHandle" @close="closeHandler" />
    </Drawer>
  </div>
</template>
<script>
import { mapState } from 'vuex';
import { dictionary } from '@/utils/dictMixin';
import { getQuestionnaireInfo, questionnaireModifyIsValid, queryQuestionPagById } from '@/api/customerRepair/visitRules/questionnaireMake2';
import QuestionnairePanel from './questionnairePanel';

export default {
  name: 'Questionnaire',
  components: {
    QuestionnairePanel
  },
  mixins: [dictionary],
  data() {
    this.BaseTable = null;
    this.QuestionTable = null;
    return {
      topFilterList: this.createTopFilterList(), // 搜索表单列表
      visible: false,
      fetchApi: getQuestionnaireInfo, // 获取答案表格接口
      fetchParams: { xhrAbort: true, dmsFuncId: 404025, isTemplate: 10041002, isValid: 10041002, dataSources: 10451001 }, // 获取答案表格参数
      columns: this.createTableColumns(), // 答案信息表格列
      getQuestionApi: queryQuestionPagById, // 获取问题表格接口
      fetchQuestionParams: { xhrAbort: true }, // 获取问题表格参数
      questionTableColums: this.createQuestionTableColums(), // 问题信息表格列
      type: '', // 新增or编辑
      editQuestionnaireId: '', // 编辑时问卷ID
      tableData: [] // 问题信息表格
    };
  },
  created() {},
  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
    this.QuestionTable = this.$refs.questionTable.$refs.pageTable;
  },
  methods: {
    createTopFilterList() {
      return [
        {
          type: 'INPUT',
          label: '问卷名称',
          fieldName: 'questionnaireName',
          placeholder: '问卷名称'
        },
        {
          type: 'SELECT',
          label: '问卷类型',
          fieldName: 'questionnaireType',
          placeholder: '问卷类型',
          itemList: this.createDictList('8003')
        },
        {
          type: 'SELECT',
          label: '是否启用',
          fieldName: 'isValid',
          placeholder: '是否启用',
          itemList: this.createDictList('1004')
        },
        {
          type: 'SELECT',
          label: '数据来源',
          fieldName: 'dataSources',
          placeholder: '数据来源',
          initialValue: 10451001,
          disabled: true,
          itemList: this.createDictList('1045')
        },
        {
          type: 'RANGE_DATE',
          label: '问卷制作日期',
          fieldName: 'questionnaireDateFrom|questionnaireDateTo',
          placeholder: '选择日期'
        }
      ];
    },
    createTableColumns() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 70,
          fixed: 'left',
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 180,
          fixed: 'left',
          render: props => {
            return (
              <div>
                <el-button size="mini" type="text" onClick={() => this.editQuestionnaire(props.row)}>
                  编辑
                </el-button>
                <el-divider direction="vertical" />
                <el-button size="mini" type="text" onClick={() => this.getQuestionsByQuestionnaireId(props.row)}>
                  查看问题
                </el-button>
                <el-divider direction="vertical" />
                <el-button size="mini" type="text" disabled={props.row.DATA_SOURCES == '10451002' || props.row.NUMBER > 0} onClick={() => this.setIsVaild(props.row)}>
                  {props.row.IS_VALID == '10041001' ? '停用' : '启用'}
                </el-button>
              </div>
            );
          }
        },
        {
          title: '问卷ID',
          dataIndex: 'QUESTIONNAIRE_ID',
          filterType: 'input'
        },
        {
          title: '问卷名称',
          dataIndex: 'QUESTIONNAIRE_NAME',
          filterType: 'input',
          showOverflowTooltip: true
        },
        {
          title: '问卷类型',
          dataIndex: 'QUESTIONNAIRE_TYPE',
          filterType: 'input',
          render: props => {
            return <div>{this.createDictText(props.row.QUESTIONNAIRE_TYPE, '8003')}</div>;
          }
        },
        {
          title: '是否启用',
          dataIndex: 'IS_VALID',
          sorter: true,
          filterType: 'input',
          render: props => {
            return <div>{this.createDictText(props.row.IS_VALID, '1004')}</div>;
          }
        },
        {
          title: '问卷制作日期',
          dataIndex: 'QUESTIONNAIRE_DATE',
          sorter: true,
          filterType: 'input'
        },
        {
          title: '数据来源',
          dataIndex: 'DATA_SOURCES',
          sorter: true,
          filterType: 'input',
          render: props => {
            return <div>{this.createDictText(props.row.DATA_SOURCES, '1045')}</div>;
          }
        }
      ];
    },
    createQuestionTableColums() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 70,
          fixed: 'left',
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '问题名称',
          dataIndex: 'QUESTION_NAME',
          filterType: 'input'
        },
        {
          title: '问题内容',
          dataIndex: 'QUESTION_CONTENT',
          filterType: 'input'
        },
        {
          title: '问题类型',
          dataIndex: 'QUESTION_TYPE',
          filterType: 'input',
          render: props => {
            return <div>{this.createDictText(props.row.QUESTION_TYPE, '8074')}</div>;
          }
        },
        {
          title: '是否必填',
          dataIndex: 'IS_MUST_FILLED',
          filterType: 'input',
          render: props => {
            return <div>{this.createDictText(props.row.IS_MUST_FILLED, '1004')}</div>;
          }
        },
        {
          title: '答案选项',
          dataIndex: 'ANSWER',
          filterType: 'input'
        },
        {
          title: '排列顺序',
          dataIndex: 'QUESTION_SORT_NUM',
          filterType: 'input'
        }
      ];
    },
    // 搜索
    changeHandle(val) {
      this.fetchParams = Object.assign({}, val, { xhrAbort: false, isTemplate: 10041002 });
      this.clearQuestionTable();
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    // 保存QuestionTable的数据，用于清空
    tableChangeFun(val) {
      this.tableData = val;
    },
    // 清空QuestionTable
    clearQuestionTable() {
      this.$nextTick(() => {
        this.QuestionTable.EXECUTE_DELETE(this.tableData);
      });
    },
    closeHandler(val) {
      this.visible = val;
    },
    // 设置启用停用
    setIsVaild(row) {
      let data = {
        isValid: row.IS_VALID === 10041001 ? 10041002 : 10041001,
        id: row.QUESTIONNAIRE_ID
      };
      questionnaireModifyIsValid(data)
        .then(() => {
          this.$message({
            type: 'success',
            message: (row.IS_VALID === 10041001 ? '停用' : '启用') + '成功!'
          });
          this.$refs.topFilter.SUBMIT_FORM();
        })
        .catch(err => {});
    },
    // 根据问卷ID查询其选项
    getQuestionsByQuestionnaireId(row) {
      let vat = {
        id: row.QUESTIONNAIRE_ID
      };
      this.fetchQuestionParams = Object.assign({}, vat, { xhrAbort: false });
    },
    // 新增问卷
    add() {
      this.visible = true;
      this.type = 'add';
      this.editQuestionnaireId = '';
    },
    // 编辑问卷
    editQuestionnaire(row) {
      this.visible = true;
      this.type = 'edit';
      this.editQuestionnaireId = row.QUESTIONNAIRE_ID + '';
    }
  }
};
</script>

<style lang="less" scoped>
.dialog-form {
  width: 100%;
  margin: 0;
}

.el-checkbox-group .el-checkbox + .el-checkbox {
  margin: 0;
}

.box {
  border: 1px solid @borderColor;
  border-radius: @borderRadius;
  box-shadow: @boxShadow;
  padding: @modulePadding;
  margin-top: 20px;
}
</style>
